Page({
  data: {
    userInfo: null,
    orderStats: {
      unpaid: 0,
      undelivered: 0,
      delivered: 0
    }
  },

  onLoad() {
    this.getUserInfo()
  },

  onShow() {
    this.getOrderStats()
  },

  // 获取用户信息
  getUserInfo() {
    const userInfo = wx.getStorageSync('userInfo')
    if (userInfo) {
      this.setData({ userInfo })
    } else {
      wx.navigateTo({
        url: '/pages/login/login'
      })
    }
  },

  // 获取订单统计
  async getOrderStats() {
    try {
      const res = await wx.request({
        url: 'http://localhost:3000/api/order/stats',
        header: {
          Authorization: `Bearer ${wx.getStorageSync('token')}`
        }
      })
      this.setData({
        orderStats: res.data
      })
    } catch (error) {
      console.error('获取订单统计失败', error)
    }
  },

  // 退出登录
  logout() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          wx.clearStorageSync()
          wx.navigateTo({
            url: '/pages/login/login'
          })
        }
      }
    })
  }
})